package my.georings.dialog;

import my.Utils;
import my.georings.GeoPhoneRingsApp;
import my.georings.MyMapActivity;
import my.georings.R;
import my.georings.data.MyLocation;
import my.georings.service.LocationChangeService;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.android.maps.MapController;

public class ShowOnMapDialog extends MyMapActivity{

	private MyLocation currLocation;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
		int theId = getIntent().getIntExtra("theId", -1);
		currLocation = ((GeoPhoneRingsApp)getApplication()).getMyLocationsManager().getLocationOfId(theId);
		firstTime = false;
		
		boundConnection = new ServiceConnection() {
		    public void onServiceConnected(ComponentName className, IBinder service) {
		    	boundService = ((LocationChangeService.LocationChangeBinder)service).getService();
		    	
		    	Location newLocation = boundService.getLastLocation();
		    	if(newLocation == null){
		    		Utils.showToast(ShowOnMapDialog.this, "The application could not get your last location.", Toast.LENGTH_SHORT);
		    	}else{
			    	onMyLocationChanged(newLocation);
		    	}
		    	boundService.addListener(ShowOnMapDialog.this);
		    }

		    public void onServiceDisconnected(ComponentName className) {
		    	boundService = null;
		    }
		};
	}
	
	protected void refreshCurrentLocationView(){
		MapController controller = mapView.getController();
		controller.setCenter(currLocation.getPoint());
		controller.setZoom(15);
		super.refreshCurrentLocationView();
	}
	
	protected void doBindService() {
		Intent myIntent = new Intent(this, my.georings.service.LocationChangeService.class);
		this.bindService(myIntent, boundConnection, Context.BIND_AUTO_CREATE);
	}

	protected void doUnbindService() {
	    if (boundService != null) {
	    	boundService.removeListener(this);
	    	this.unbindService(boundConnection);
	        boundService = null;
	    }
	}
	
	@Override
	protected void initSpinner(){
		Spinner spinner = (Spinner) findViewById(R.id.map_filter);
	    spinner.setVisibility(View.GONE);
	}
	

}
